$(function() {
	//初始化表格
	easyExt.initDataGrid('#dg','/role/list');
	easyExt.initTreeGrid('#tg','/resource/findAllPermission'); 
	//搜索实现
	$("#search").click(function(){
		$('#dg').datagrid('load',$('#tForm').serializeJson());
	});
	//删除实现
	$("#del").click(function(){
		var selRows=$('#dg').datagrid('getSelections');
		easyExt.del(selRows,'/role/delete',function(){
			$('#dg').datagrid('clearSelections'); 
			$('#dg').datagrid('reload'); 
		});
	});
	//添加实现
	$("#add").click(function(){
		easyExt.add('/role/add',function(){
			$('#dg').datagrid('reload'); 
		});
	});
	//修改实现
	$("#edit").click(function(){
		var selRows=$('#dg').datagrid('getSelections');
		easyExt.edit(selRows,'/role/edit',function(){
			$('#dg').datagrid('clearSelections'); 
			$('#dg').datagrid('reload'); 
		});
	});
});

var role=role||{};
//查看权限
function lookPermission(id){
	role.id=id;
	$('#dg').datagrid('clearSelections');
	easyExt.ajax({url:easyExt.url+"/role/getRolePermission?id="+id,type:'GET'},function(data,status, xhr){
		$('#tg').treegrid("unselectAll");
		for(var i=0;j=data.length,i<j;i++){
			$('#tg').treegrid("select",data[i].id);
		}
	});
}
function formatter(value, row, index){
	return '<a href="javascript:lookPermission('+row.id+')"><div class="icon-hamburg-lock" style="width:16px;height:16px" title="查看权限"></div></a>';
}
//保存角色权限
function saveRolePermission(){
	var p=$('#tg').treegrid("getSelections");
	var pIds="";
	for(var i=0;j=p.length,i<j;i++){
		pIds+=p[i].id+",";
	}
	easyExt.ajax({url:easyExt.url+"/role/saveRolePermission?roleId="+role.id+"&resourceIds="+pIds},function(data, status, xhr){
		if(data){
			$.messager.alert("提示", "保存权限成功！", "info");
		}
	});
}
